/*
RetroLeague - Mobile application for The Retro League Podcast
Copyright (C) 2010-2013 Hugues Johnson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.retroleague;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import com.huguesjohnson.retroleague.R;
import com.huguesjohnson.retroleague.util.Utils;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class StreamEntryArrayAdapter extends ArrayAdapter<StreamEntry>{
	private final static int MAX_LIST=50; //the maximum number of items stored in this list
	private Context context;
	private int resourceId;
	private ArrayList<StreamEntry> entries;
	SimpleDateFormat dateFormat;
	
	public StreamEntryArrayAdapter(Context context,int resourceId,ArrayList<StreamEntry> entries){
		super(context,resourceId,(StreamEntry[])entries.toArray(new StreamEntry[entries.size()]));
		this.context=context;
		this.resourceId=resourceId;
		this.entries=entries;
		this.dateFormat=new SimpleDateFormat("MM/dd/yyyy HH:mm:ss",Locale.getDefault());
		//sloppy step in case I was sloppy somewhere else
		this.trim();
	}
	
	public ArrayList<StreamEntry> getEntries(){
		return(this.entries);
	}
	
	private void trim(){
		if((this.entries!=null)&&(this.entries.size()>MAX_LIST)){
			this.entries.subList(MAX_LIST,this.entries.size()-1).clear();
		}
	}
	
	public Date getMostRecent(){
		if(this.entries.size()<1){
			try{
				return(this.dateFormat.parse("01/01/1980 12:00:00"));
			}catch (ParseException e){
				//yes - it would be a problem if this happened but it can't unless we're in bizarro Java world, which I guess means it's totally possible
				return(new Date());
			}
		}else{
			return(this.entries.get(0).getPostedDate());
		}
	}
	
    public void appendListToFront(ArrayList<StreamEntry> entries){
    	this.entries.addAll(0,entries);
		this.trim();
    }
	
	@Override
	public int getCount(){
	    return(this.entries.size());
	}

	@Override
	public StreamEntry getItem(int position) {
	    return(this.entries.get(position));
	}

	@Override
	public long getItemId(int position){ 
	    return(position);
	}
	
	@Override
	public View getView(int position,View convertView,ViewGroup parent){
		try{
			//inflate the layout
			LayoutInflater inflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View layout=inflater.inflate(resourceId,null);
			//title
			StreamEntry entry=this.entries.get(position);
			TextView title=(TextView)layout.findViewById(R.id.listitem_stream_title);
			title.setText(entry.getTitle());
			//posted date
			TextView postedDate=(TextView)layout.findViewById(R.id.listitem_stream_date);
			postedDate.setText(this.dateFormat.format(entry.getPostedDate()));
			//author
			String author=entry.getAuthor();
			if((author==null)||(author.length()<1)){
				TextView tvauthor=(TextView)layout.findViewById(R.id.listitem_stream_author);
				TextView tvauthorlabel=(TextView)layout.findViewById(R.id.listitem_stream_label_author);
				tvauthor.setVisibility(View.GONE);
				tvauthorlabel.setVisibility(View.GONE);
			}else{
				TextView tvauthor=(TextView)layout.findViewById(R.id.listitem_stream_author);
				tvauthor.setText(author);
			}
			//icon
			ImageView icon=(ImageView)layout.findViewById(R.id.listitem_stream_icon);
			icon.setImageDrawable(this.context.getResources().getDrawable(Utils.getSourceIconId(entry.getSource())));
			//done
			return(layout);
		}catch(Exception x){
			Log.e("EpisodeListArrayAdapter.getView",x.getMessage(),x);
			return(null);
		}
	}
}